package com.skywolf.chem.live.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.sysconfig.pojo.dto.PageDTO;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.live.entity.Video;
import com.skywolf.chem.live.entity.Videocomment;
import com.skywolf.chem.live.entity.Videoitem;
import com.skywolf.chem.live.mapper.VideoMapper;
import com.skywolf.chem.live.param.VideoDto;
import com.skywolf.chem.live.param.VideoInfoDto;
import com.skywolf.chem.live.service.IVideoService;
import com.skywolf.chem.live.service.IVideocommentService;
import com.skywolf.chem.live.service.IVideoitemService;
import com.skywolf.chem.manager.param.VideoQueryDto;
import com.skywolf.chem.tags.entity.Tags;
import com.skywolf.chem.tags.service.ITagsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 视频信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-11-30
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    @Autowired
    VideoMapper videoMapper;

    @Autowired
    IVideocommentService commentService;

    @Autowired
    IVideoitemService videoItemService;

    @Autowired
    ITagsService tagsService;

    @Override
    public PageVO<VideoDto> pageVideoList(PageDTO queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<VideoDto> list = videoMapper.getVideoPageList(startNum,endNum,1,null,null,null);
        Integer count = videoMapper.getVideoPageCount(1,null,null,null);
        PageVO<VideoDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public VideoInfoDto getVideoInfo(Long videoId) {
        Video video = this.getById(videoId);
        if(video==null){
            return null;
        }
        VideoInfoDto dto = new VideoInfoDto();
        BeanUtils.copyProperties(video,dto);
        dto.setUserName(video.getSpeaker());
        QueryWrapper<Videocomment> queryWrapper=new QueryWrapper();
        queryWrapper.eq("video_id",videoId);
        Integer commentCount = commentService.count(queryWrapper);
        dto.setCommentCount(commentCount);

        QueryWrapper<Videoitem> wrapper = new QueryWrapper<>();
        wrapper.select("id", "video_title", "video_url", "video_time").eq("video_id", videoId);
        List<Videoitem> videoitems = videoItemService.list(wrapper);
        dto.setUrlList(videoitems);

        Tags tag = tagsService.getById(video.getTagId());
        if(tag!=null){
            dto.setTagName(tag.getTname());
        }
        return dto;
    }

    @Override
    public PageVO<VideoDto> pageVideoListManager(VideoQueryDto queryParam) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<VideoDto> list = videoMapper.getVideoPageList(startNum,endNum,queryParam.getStatus(),queryParam.getTitle(),queryParam.getStartTime(),queryParam.getEndTime());
        Integer count = videoMapper.getVideoPageCount(queryParam.getStatus(),queryParam.getTitle(),queryParam.getStartTime(),queryParam.getEndTime());
        PageVO<VideoDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public VideoDto getVideoManagerInfo(Long id) {
        return videoMapper.getVideoInfo(id);
    }
}
